package com.danger.Activity.Account;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Model.bean.UserInfoBean;
import com.danger.Util.FileCacheService;
import com.danger.Util.ImageUtil.ImageLoaderUtil;
import com.danger.Widget.MyCircleImageView;
import com.nostra13.universalimageloader.core.assist.ImageSize;

import cn.bmob.v3.BmobUser;

/**
 * Created by danger on 2016/4/25.
 */
public class ScrollingUserInfoFragment extends BackHandledFragment implements View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private Toolbar toolbar;
    private RelativeLayout modify_userphoto_linear;
    private MyCircleImageView origin_userphoto_circleImageView;
    private RelativeLayout modify_username_linear;
    private TextView origin_username_textView;
    private RelativeLayout modify_password_linear;
    private RelativeLayout modify_email_linear;
    private TextView origin_email_textView;
    private RelativeLayout modify_phone_linear;
    private TextView origin_phone_textView;
    private UserInfoActivity activity;
    private FileCacheService service;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_userinfo_scroll_theme, container, false);

        initView();
        initEvent();

        return view;
    }

    private void initView() {
        activity = (UserInfoActivity) getActivity();
        toolbar = activity.getToolbar();
        toolbar.setTitle("个人资料");
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        service = new FileCacheService(activity);
        modify_userphoto_linear = (RelativeLayout) view.findViewById(R.id.modify_userphoto_linear);
        origin_userphoto_circleImageView = (MyCircleImageView) view.findViewById(R.id.origin_userphoto_circleImageView);
        modify_username_linear = (RelativeLayout) view.findViewById(R.id.modify_username_linear);
        origin_username_textView = (TextView) view.findViewById(R.id.origin_username_textView);
        modify_password_linear = (RelativeLayout) view.findViewById(R.id.modify_password_linear);
        modify_email_linear = (RelativeLayout) view.findViewById(R.id.modify_email_linear);
        origin_email_textView = (TextView) view.findViewById(R.id.origin_email_textView);
        modify_phone_linear = (RelativeLayout) view.findViewById(R.id.modify_phone_linear);
        origin_phone_textView = (TextView) view.findViewById(R.id.origin_phone_textView);
        UserInfoBean userInfo = BmobUser.getCurrentUser(activity, UserInfoBean.class);
        if (service.getSharePreference("setting").get("theme").equals("2")) {
            activity.getUserNameTexTView().setText(userInfo.getUsername().toString());
            activity.getUserPhoneTextView().setText(userInfo.getMobilePhoneNumber().toString());
            if (userInfo.getPhoto() != null) {
                new ImageLoaderUtil(new ImageSize(90, 90), activity.getMyPhotoView(), userInfo.getPhoto(), activity).load();
            }
        }
        if (userInfo.getPhoto() == null) {
            origin_userphoto_circleImageView.setImageResource(R.drawable.photo);
        } else {
            new ImageLoaderUtil(new ImageSize(30, 30), origin_userphoto_circleImageView, userInfo.getPhoto(), activity).load();
        }
        origin_username_textView.setText(userInfo.getUsername());
        origin_email_textView.setText(userInfo.getEmail());
        origin_phone_textView.setText(userInfo.getMobilePhoneNumber());
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        modify_userphoto_linear.setOnClickListener(this);
        modify_username_linear.setOnClickListener(this);
        modify_password_linear.setOnClickListener(this);
        modify_email_linear.setOnClickListener(this);
        modify_phone_linear.setOnClickListener(this);
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.modify_userphoto_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.user_info_container, new ModifyPhotoFragment()).commit();
                break;
            case R.id.modify_username_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.user_info_container, new ModifyUsernameFragment()).commit();
                break;
            case R.id.modify_password_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.user_info_container, new ModifyPasswordFragment()).commit();
                break;
            case R.id.modify_email_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.user_info_container, new ModifyEmailFragment()).commit();
                break;
            case R.id.modify_phone_linear:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.user_info_container, new ModifyPhoneFragment()).commit();
                break;
            default:
                break;
        }
    }
}
